package com.niiwoo.civet.trade.listener;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.activity.stage.dto.request.inviteBuddy.UserInvestEventDTO;
import com.niiwoo.activity.stage.service.inviterBuddy.InviteBuddyActivityDubboService;
import com.niiwoo.civet.trade.constant.RabbitConstant;
import com.niiwoo.tripod.provider.exception.BizException;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by 李想 on 2017/12/15.
 */
@Slf4j
@Component
public class InvestEventForActivityListener {
	@Reference(version = "1.0.0")
	private InviteBuddyActivityDubboService inviteBuddyActivityDubboService;

	@RabbitListener(queues = {RabbitConstant.Queue.INVEST_EVENT_FOR_ACTIVITY})
	public void investEventSend(UserInvestEventDTO request, Message message, Channel channel) throws IOException {
		log.info("投资事件推送至活动" + request);
		Boolean isAck = true;
		try {
			inviteBuddyActivityDubboService.inviteBestirActivityInvest(request);
			log.info("投资事件推送至活动成功，{}", request);
		} catch (BizException e) {
			log.error("投资事件推送至活动业务异常，{}", request, e);
		} catch (Exception e) {
			log.error("投资事件推送至活动调用异常，{}", request, e);
			isAck = false;
			try {
				Thread.sleep(5 * 1000L);
			} catch (Exception expect) {
			}
		} finally {
			if (isAck) {
				channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			} else {
				channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
			}
		}
	}
}
